<?php
require_once("lib.php");

// Basic set has occured.
// Use $_SESSION["authed"] to test for auth
// Use $db to access mysqli API to database

if (isset($_GET["dt"])) {
    $_SESSION["disabledTags"][$_GET["dt"]] = true;
}

if (isset($_GET["et"])) {
    unset($_SESSION["disabledTags"][$_GET["et"]]);
}

if (isset($_GET["ena"])) {
    unset($_SESSION["disabledTags"]);
}

if (!isset($_SESSION["disabledTags"])) {
    $_SESSION["disabledTags"] = Array();
}
$enabledTags = Array();
$tagList = "";
$stmt = $db->prepare("SELECT * FROM track_tags ORDER BY trackTag");
if ($stmt->execute()) {
    while ($row = $stmt->fetch()) {
        $tag = $row["trackTag"];
        $tagID = $row["id"];
        if (isset($_SESSION["disabledTags"][$tagID])) {
            $tagList .= "<a href=\"?et=$tagID\"><span class=\"label label-important\">$tag</span></a>&nbsp";
        } else {
            $enabledTags[] = $tagID;
            $tagList .= "<a href=\"?dt=$tagID\"><span class=\"label label-success\">$tag</span></a>&nbsp";
        }
    }
}

$stmt = $db->prepare("SELECT t.id AS trackID, t.trackName AS trackName, t.userDescr AS trackDescr, t.userPhotoAlbumURL AS photoURL, t.userHikeLogURL AS hikeLogURL, UNIX_TIMESTAMP(t.trackDate) AS trackDate, t.trackFileURL as trackFileURL, GROUP_CONCAT(DISTINCT tt.trackTag ORDER BY tt.trackTag SEPARATOR '||') AS tags FROM track_tables_info t LEFT JOIN track_tags_link ttl ON ttl.tracksID = t.id LEFT JOIN track_tags tt ON tt.id = ttl.trackTagID GROUP BY t.id ORDER BY trackDate DESC");

/* Disabled...
$filterList = a list of track IDs... comma seperated
$filterListCount = a count of items in $filterList

$filterListArray = array(2,6);
$filterList = implode(",", $filterListArray);
$filterListCount = count($filterListArray);

SELECT t.id AS trackID, t.trackName AS trackName, t.userDescr AS trackDescr, t.userPhotoAlbumURL AS photoURL, t.userHikeLogURL AS hikeLogURL, UNIX_TIMESTAMP(t.trackDate) AS trackDate, GROUP_CONCAT(DISTINCT tt.trackTag ORDER BY tt.trackTag SEPARATOR '||') AS tags FROM track_tables_info t LEFT JOIN track_tags_link ttl ON ttl.tracksID = t.id LEFT JOIN track_tags tt ON tt.id = ttl.trackTagID WHERE tt.id IN ($filterList) GROUP BY t.id HAVING COUNT(DISTINCT tt.id) = $filterListCount ORDER BY trackDate DESC;
*/

$tracks = "";
if ($stmt->execute()) {
    $trackCount = $stmt->rowCount();
    while ($row = $stmt->fetch()) {
        $tags = explode("||", $row["tags"]);
        $trackID = $row["trackID"];
        $trackName = $row["trackName"];
        $trackDate = date("c", $row["trackDate"]);
        $trackDescr = $row["trackDescr"];
        $photoURL = ($row["photoURL"] == "" ? "--" : $row["photoURL"]);
        $hikeLogURL = ($row["hikeLogURL"] == "" ? "--" : $row["hikeLogURL"]);
        $trackFileURL = ($row["trackFileURL"] == "" ? "--" : $row["trackFileURL"]);
        $tagStr = "";
        $directLink = base64_encode(json_encode(Array("multitracks" => Array($trackID))));
        foreach ($tags as $tag) {
            $tagStr .= "<span class=\"label label-info\">$tag</span>&nbsp;";
        }
        $tracks .= sprintf("<tr><td><input name=\"tracks[%s]\" type=\"checkbox\"></td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td NOWRAP>%s</td><td>%s</a></td><td>%s</td><td>%s</td></tr>",
            $trackID,
            sprintf("<a href=\"map.php?tracks=%s\"><i class=\"icon-map-marker\"></i></a>", $directLink),
            (filter_var($trackFileURL, FILTER_VALIDATE_URL) ? sprintf("<a target=\"_new\" href=\"%s\"><i class=\"icon-download\"></i></a>", $trackFileURL) : $trackFileURL),
            (filter_var($photoURL, FILTER_VALIDATE_URL) ? sprintf("<a target=\"_new\" href=\"%s\"><i class=\"icon-picture\"></i></a>", $photoURL) : $photoURL),
            (filter_var($hikeLogURL, FILTER_VALIDATE_URL) ? sprintf("<a target=\"_new\" href=\"%s\"><a class=\"icon-align-left\"></i></a>", $hikeLogURL) : $hikeLogURL),
            ($_SESSION["authed"] ? sprintf("<a href=\"editTrack.php?t=%s\"><i class=\"icon-edit\"></i></a>", $trackID) : ""),
            sprintf("<a href=\"trackDetails.php?t=%s\">%s</a>", $trackID, $trackDate),
            $trackName,
            $tagStr);
    }
}


$authedNavs = "";
if ($_SESSION["authed"]) {
    $authedNavs = <<< AUTHNAVS
<li class="divider-vertical"></li>
<li>
    <a href="editTrackTags.php">Manage Tags</a>
</li>
<li class="divider-vertical"></li>
<li>
    <form name="importFile" enctype="multipart/form-data" action="importGPX.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="10485760" />
        <button type="submit" class="btn">upload</button>
        <input name="userfile" type="file" />
    </form>
</li>
AUTHNAVS;
}

$htmlTitle = "Hikes and Things!";
$nickname = $_SESSION["nickname"];
echo <<<CONTENT
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>$htmlTitle</title>
        <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
        <link href="style.css" rel="stylesheet">
        $analytics
    </head>
    <body>
        <div class="navbar navbar-static">
            <div class="navbar-inner">
                <div class="container" style="width: auto;">
                    <a class="brand" href="#">Tracks</a>
                    <ul class="nav" role="navigation">
                        <li class="divider-vertical"></li>
                        <li><a href="map.php">Topo Map</a></li>
                        $authedNavs
                    </ul>
                    <ul class="nav pull-right">
                        <li class="divider-vertical"></li>
                        <li><p class="navbar-text"><span class="label">$nickname</span></p></li>
                        <li class="divider-vertical"></li>
                        <li><p class="navbar-text">$trackCount tracks</p></li>
                        <li class="divider-vertical"></li>
                        <li>$logInOut</li>
                    </ul>
                </div>
            </div>
        </div>
        <form action="map.php" method="post">
            <div class="well">
                <p>View multiple tracks on a single map; check each track you wish to map, then click this button ===> <button type="submit" class="btn">map!</button></p>
                <p>Otherwise, to view a single track, click the <i class="icon-map-marker"></i> icon</p>
            </div>
<!--            <div class="well"<>
                <h3>Tags</h3>
                <p>
                    <a href="?ena"><span class="label label-success">Enable All Tags</span></a>
                </p>
                $tagList
-->            </div>
            <table class="table table-striped table-condensed">
                <thead>
                    <tr>
                        <th class="center">Map</th>
                        <th class="center"></th>
                        <th class="center"></th>
                        <th class="center"></th>
                        <th class="center"></th>
                        <th class="center"></th>
                        <th class="center">Date</th>
                        <th class="center">Name</th>
                        <th class="center">Tags</th>
                    </tr>
                </thead>
                <tbody>
                    $tracks
                </tbody>
            </table>
        </form>
        <script src="jquery-1.9.1.min.js"></script>
        <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
    </body>
</html>
CONTENT;
?>
